﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Combine : MonoBehaviour
{
    public Transform source;
    public Transform target;
    private Dictionary<string, Dictionary<string, SkinnedMeshRenderer>> data = new Dictionary<string, Dictionary<string, SkinnedMeshRenderer>>();
    // Use this for initialization
    void Start()
    {
        SkinnedMeshRenderer[] parts = source.GetComponentsInChildren<SkinnedMeshRenderer>(true);
        foreach (SkinnedMeshRenderer part in parts)
        {
            string[] partName = part.name.Split('-');
            if (!data.ContainsKey(partName[0])) data.Add(partName[0], new Dictionary<string, SkinnedMeshRenderer>());
            data[partName[0]].Add(partName[1], part);
        }
        SkinnedMeshRenderer targetSmr = target.gameObject.AddComponent<SkinnedMeshRenderer>();
        Transform[] hips = target.GetComponentsInChildren<Transform>();
        List<CombineInstance> combineInstances = new List<CombineInstance>();
        List<Material> materials = new List<Material>();
        List<Transform> bones = new List<Transform>();
        foreach (KeyValuePair<string, Dictionary<string, SkinnedMeshRenderer>> _part in data)
        {

            //从资料中取得各部位指定编号的 SkinnedMeshRenderer
            SkinnedMeshRenderer smr = new SkinnedMeshRenderer();
            switch (_part.Key)
            {
                case "eyes":
                    smr = _part.Value["1"];
                    break;
                case "face":
                    smr = _part.Value["1"];
                    break;
                case "hair":
                    smr = _part.Value["1"];
                    break;
                case "pants":
                    smr = _part.Value["1"];
                    break;
                case "shoes":
                    smr = _part.Value["1"];
                    break;
                case "top":
                    smr = _part.Value["1"];
                    break;

            }
            //准备要组合的 Mesh

            CombineInstance ci = new CombineInstance();

            ci.mesh = smr.sharedMesh;

            combineInstances.Add(ci);

            //排列新的材质列表

            materials.AddRange(smr.materials);
            foreach (Transform bone in smr.bones)
            {
                foreach (Transform hip in hips)
                {
                    if (hip.name != bone.name) continue;
                    bones.Add(hip);
                    break;
                }
            }
        }
        //合并 Mesh 并写入至 Target 的 SkinnedMeshRenderer

        targetSmr.sharedMesh = new Mesh();

        targetSmr.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);

        // Target 的 SkinnedMeshRenderer 写入新骨架列表


        targetSmr.bones = bones.ToArray();

        // Target 的 SkinnedMeshRenderer 写入新材质列表


        targetSmr.materials = materials.ToArray();

        /** 重组模型 结束 */

        //指定播放走路动作


    }
    // Update is called once per frame
    void Update()
    {

    }
}
